//
//  FZNavigationController.swift
//  MyWeiBo
//
//  Created by fangzhengwei on 2017/3/26.
//  Copyright © 2017年 fangzhengwei. All rights reserved.
//

import UIKit

class FZNavigationController: UINavigationController, UIGestureRecognizerDelegate {

        
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.interactivePopGestureRecognizer?.delegate = self
        
    }

      
    
}



extension FZNavigationController {
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        super.pushViewController(viewController, animated: true)
        
        let title = viewControllers.first?.navigationItem.title
        if viewControllers.count > 1{
            
            if viewControllers.count == 2 {
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title:title!, target: self, selector: #selector(ToViewController), imgName: "navigationbar_back_withtext")
                
                
            }else if (viewControllers.count > 2){
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title:"返回", target: self, selector: #selector(ToViewController))
                
            }
            
            viewController.tabBarController?.tabBar.isHidden = true
        }else{
            
            viewController.tabBarController?.tabBar.isHidden = false
            
        }
    }
    func ToViewController() {
        
        popViewController(animated: true)
        
    }
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        if self.viewControllers.count == 1 {
            
            return false
        }
        return true
    }

    
    
}









